package com.example.project.dao;

import com.example.project.pojo.User;
import com.example.project.util.DBHelper;
import org.apache.commons.codec.digest.DigestUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDAO {
    //根据名称查看用户
    public List<User> findUserByUserName(String userName)
    {
       List<User> userList = new ArrayList<>();


        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        Connection connection = DBHelper.getConnection();

        String sql="select * from user where username=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            resultSet= preparedStatement.executeQuery();
            while(resultSet.next()) {
                User user= new User();
                user.setId(resultSet.getInt("id"));
                user = findUserByID(user.getId());
                userList.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBHelper.close(resultSet,preparedStatement,connection);
        }

        return userList;
    }
    //用户电话查询
    public User findUserByUserPhone(String userPhone)
    {
        User user = new User();


        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        Connection connection = DBHelper.getConnection();

        String sql="select * from user where userphone=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userPhone);
           resultSet= preparedStatement.executeQuery();
           while(resultSet.next()){
               user.setId(resultSet.getInt("id"));
               user=findUserByID(user.getId());
           }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBHelper.close(resultSet,preparedStatement,connection);
        }
        return user;
    }

    //重置密码
    public boolean ResetPassword(String newpassword, int id) {
        Connection connection = DBHelper.getConnection();
        PreparedStatement ps = null;

        String sql = "update user set userpass=?where id=?";
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, DigestUtils.md5Hex(newpassword));
            ps.setInt(2, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBHelper.close(ps, connection);
        }
        return true;

    }
    public User findUserByID(Integer id){
        Connection connection = DBHelper.getConnection();
        PreparedStatement ps=null;
        ResultSet rs =null;
        String sql ="select * from user where id=?";
        User user =new User();
        try {

            ps =connection.prepareStatement(sql);
            ps.setInt(1,id);
            rs=ps.executeQuery();
            while( rs.next()) {

                user.setUserPass(rs.getString("userpass"));
                user.setUserFace(rs.getString("userface"));
                user.setUserPhone(rs.getString("userphone"));
                user.setUserName(rs.getString("username"));
                user.setTotalMoney(rs.getDouble("totalmoney"));
                user.setHouseNumber(rs.getString("house_number"));
                user.setId(id);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBHelper.close(rs,ps,connection);
        }

        return user;
    }
    //用户注册
    public void Register(User user) {
        Connection connection = DBHelper.getConnection();
        PreparedStatement ps = null;
        String sql = "insert into user(username,userphone,userpass,userface,house_number) values(?,?,?,?,?)";
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUserName());
            ps.setString(2, user.getUserPhone());
            ps.setString(3, DigestUtils.md5Hex(user.getUserPass()));
            ps.setString(4, user.getUserFace());
            ps.setString(5, user.getHouseNumber());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBHelper.close(ps, connection);
        }
    }

    public Boolean FindPassBeforeUpdate(User user) {
        Connection connection = DBHelper.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select userpass from user where id =?";
        try {
            ps = connection.prepareStatement(sql);
            ps.setInt(1, user.getId());
            rs = ps.executeQuery();
            while (rs.next()) {
                if (user.getUserPass().equals(rs.getString("userpass"))||user.getUserPass().isEmpty())
                    return false;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    public void UserUpdate(User user) {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = null;
        String sql = "update user set userphone=?,username =?,userpass=?,userface=?,house_number=? where id=?";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUserPhone());
            ps.setString(2, user.getUserName());
            if (FindPassBeforeUpdate(user)) {
                ps.setString(3, DigestUtils.md5Hex(user.getUserPass()));
            } else {
                ps.setString(3, user.getUserPass());
            }
            ps.setString(4, user.getUserFace());
            ps.setString(5, user.getHouseNumber());
            ps.setInt(6, user.getId());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Boolean CheckUserPhoneBeforeRegister(String userphone) {


        Connection connection = DBHelper.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from user where userphone=?";
        try {
            ps = connection.prepareStatement(sql);
            ps.setString(1, userphone);
            rs = ps.executeQuery();
            while (rs.next()) {
                return false;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBHelper.close(rs, ps, connection);
        }
        return true;
    }

    public User login(User user) {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from user where userphone=?";
        User user1 = new User();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUserPhone());
            rs = ps.executeQuery();
            while (rs.next()) {
                Integer id = rs.getInt("id");
                String username = rs.getString("username");
                String userpass = rs.getString("userpass");
                String userface = rs.getString("userface");
                String houseNumber = rs.getString("house_number");
                if (DigestUtils.md5Hex(user.getUserPass()).equals(userpass)) {
                    user1.setUserPass(userpass);
                    user1.setUserPhone(user.getUserPhone());
                    user1.setUserFace(userface);
                    user1.setUserName(username);
                    user1.setHouseNumber(houseNumber);
                    user1.setId(id);
                    user1.setTotalMoney(rs.getDouble("totalmoney"));
                } else
                    user1.setUserPhone("");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBHelper.close(rs, ps, conn);
        }

        return user1;
    }

    public void charge(double money,User user){
        Connection conn =DBHelper.getConnection();
        PreparedStatement ps =null;
        String sql ="update user set totalmoney=? where id =?";
        try {
            ps =conn.prepareStatement(sql);
            ps.setDouble(1,user.getTotalMoney()+money);
            ps.setInt(2,user.getId());
            ps.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBHelper.close(ps,conn);
        }
    }

    public List<User> findAll() {

        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> userList = new ArrayList<>();

        try {

            String sql = "select * from user ";
            ps = conn.prepareStatement(sql);

            rs = ps.executeQuery();

            while (true) {
                boolean flag = rs.next();
                if (!flag) {
                    break;
                }
                String username = rs.getString("username");
                String userphone = rs.getString("userphone");
                String userface = rs.getString("userface");
                double totalmoney=rs.getDouble("totalmoney");
                int id = rs.getInt("id");
                User user = new User();
                user.setUserName(username);
                user.setTotalMoney(totalmoney);
                user.setUserPhone(userphone);
                user.setUserFace(userface);
                user.setId(id);
                user.setHouseNumber(rs.getString("house_number"));
                userList.add(user);

            }

            return userList;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            DBHelper.close(rs, ps, conn);
        }

        return null;
    }
    public double findTotalMoneyByID(Integer id){
       Connection connection = DBHelper.getConnection();
       PreparedStatement ps =null;
       ResultSet rs =null;
       String sql ="select totalmoney from user where id =?";
       double money =0;
        try {
            ps= connection.prepareStatement(sql);
            ps.setInt(1,id);
            rs=ps.executeQuery();
            rs.next();
            money=rs.getDouble("totalmoney");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        finally {
            DBHelper.close(rs,ps,connection);
        }
        return money;
    }


}
